//
//  BaseCollectionView.m
//  BasicProject
//
//  Created by 马军凯 on 2023/4/13.
//

#import "BaseCollectionView.h"
#import "BaseFlowLayout.h"

@interface BaseCollectionView ()
<UICollectionViewDataSource, UICollectionViewDelegate,
BaseCustomLayoutDelegate>
//layout
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

@implementation BaseCollectionView
//初始化
//简单版本
- (instancetype)initWithFrame:(CGRect)frame
                    direction:(UICollectionViewScrollDirection)direction
                     itemSize:(CGSize)itemSize
                    rowMargin:(CGFloat)rowMargin
                 columnMargin:(CGFloat)columnMargin
                 sectionInset:(UIEdgeInsets)sectionInset
{
    //
    UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
    layout.scrollDirection = direction;
    //itemSize
    if (CGSizeEqualToSize(itemSize, CGSizeZero)) {
        layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
    } else {
        layout.itemSize = itemSize;
    }
    //行与行之间的间距
    layout.minimumLineSpacing = rowMargin;
    //列与列之间的间距
    layout.minimumInteritemSpacing = columnMargin;
    //上下左右的距离
    layout.sectionInset = sectionInset;
    //
    self.layout = layout;
    //
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        //创建
        [self createCollectionView];
        [self reloadData];
    }
    return self;
}
//复杂版本
- (instancetype)initWithFrame:(CGRect)frame direction:(UICollectionViewScrollDirection)direction {
    UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
    layout.scrollDirection = direction;
    layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
    
    self.layout = layout;
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        [self createCollectionView];
    }
    return self;
}
//瀑布流
- (instancetype)initAutoLayoutWithFrame:(CGRect)frame {
    BaseFlowLayout *layout = BaseFlowLayout.new;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.delegate = self;
    
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        //
        [self createCollectionView];
    }
    return self;
}

#pragma mark 创建UICollectionView
- (void)createCollectionView {
    self.dataSource = self;
    self.delegate = self;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.backgroundColor = [UIColor whiteColor];
}
#pragma mark UICollectionView dataSource
//几组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sections ?: 1;
}
//每组个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.rows) {
        NSInteger sectionNum = 1;
        if (self.sections) {
            sectionNum = self.sections;
        }
        if (sectionNum != self.rows.count) {
            NSLog(@"组和数据不一致");
            return 0;
        }
        NSNumber *num = self.rows[section];
        return [num integerValue];
    }
    return 0;
}
#pragma mark UICollectionView delegate
//cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.CreateCell(indexPath);
}
//cell的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.CellSize) {
        return self.CellSize(indexPath);
    }
    return self.layout.itemSize;
}
#pragma mark UICollection Layout
//调整Item的位置 使Item不紧挨着屏幕
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (self.ItemPadding) {
        return self.ItemPadding(section);
    }
    return self.layout.sectionInset;
}
//设置每个item水平间距 列与列之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (self.ColumnMargin) {
        return self.ColumnMargin(section);
    }
    return self.layout.minimumInteritemSpacing;
}
//设置每个item垂直间距 行与行之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (self.RowMargin) {
        return self.RowMargin(section);
    }
    return self.layout.minimumLineSpacing;
}
#pragma mark UICollectionView didSelect
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.SelectedCell) {
        self.SelectedCell(indexPath);
    }
}
#pragma mark header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        if (self.CreateHeader) {
            return self.CreateHeader(indexPath);
        }
    }
    if (kind == UICollectionElementKindSectionFooter) {
        if (self.CreateFooter) {
            return self.CreateFooter(indexPath);
        }
    }
    return [UICollectionReusableView new];
}
//段头尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (self.HeaderHeight) {
        return self.HeaderHeight(section);
    }
    return CGSizeZero;
}
//段尾尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (self.FooterHeight) {
        return self.FooterHeight(section);
    }
    return CGSizeZero;
}


#pragma mark 瀑布流
//多少列
- (NSInteger)columnCountInLayout:(BaseFlowLayout *)layout {
    return self.columnCount ?: 2;
}
//每个item的高度
- (CGFloat)layout:(BaseFlowLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    //组数
    NSInteger sectionNum = 1;
    if (self.sections) {
        sectionNum = self.sections;
    }
    if (self.cellHeightArray.count != sectionNum) {
        NSLog(@"高度的值和组数不一致");
        return 0;
    }
    //每个组的个数
    NSNumber *num = self.rows[indexPath.section];
    NSArray *heightArray = self.cellHeightArray[indexPath.section];
    if (heightArray.count != [num integerValue]) {
        NSLog(@"高度的个数和每组的个数不一致");
        return 0;
    }
    NSNumber *number = heightArray[indexPath.row];
    return [number floatValue];
}
//每行之间的间距
- (CGFloat)rowMarginInLayout:(BaseFlowLayout *)layout forSectionAtIndex:(NSInteger)section {
    if (self.RowMargin) {
        return self.RowMargin(section);
    }
    return 10;
}
//每列之间的间距
- (CGFloat)columnMarginInLayout:(BaseFlowLayout *)layout forSectionAtIndex:(NSInteger)section {
    if (self.ColumnMargin) {
        return self.ColumnMargin(section);
    }
    return 10;
}
//每个item的内边距
- (UIEdgeInsets)itemPaddingEdgesInLayout:(BaseFlowLayout *)layout forSectionAtIndex:(NSInteger)section {
    if (self.ItemPadding) {
        return self.ItemPadding(section);
    }
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
//是否设置头
- (BOOL)settingHeadLayout:(BaseFlowLayout *)layout {
    if (self.CreateHeader) {
        return YES;
    }
    return NO;
}
//头的尺寸
- (CGSize)headViewHeightWithLayout:(BaseFlowLayout *)layout forItemWith:(nonnull NSIndexPath *)indexPath {
    if (self.HeaderHeight) {
        return self.HeaderHeight(indexPath.section);
    }
    return CGSizeZero;
}
//是否设置尾
- (BOOL)settingFootLayout:(BaseFlowLayout *)layout {
    if (self.CreateFooter) {
        return YES;
    }
    return NO;
}
//尾的尺寸
- (CGSize)footViewHeightWithLayout:(BaseFlowLayout *)layout forItemWith:(NSIndexPath *)indexPath {
    if (self.FooterHeight) {
        return self.FooterHeight(indexPath.section);
    }
    return CGSizeZero;
}

@end
